﻿namespace Blaze.UI
{
    using UnityEngine;
    using UnityEngine.Sprites;
    using UnityEngine.UI;

    /// <summary>
    /// 使用Alpha遮罩的图片组件。
    /// </summary>
    public class AlphaMaskImage : FastImage
    {
        protected override void OnPopulateMesh(VertexHelper vh)
        {
            if (color.a == 0f)
            {
                vh.Clear();
                return;
            }

            var activeSprite = ActiveSprite;
            var drawingDimensions = GetDrawingDimensions(true);
            var sv1 = !(activeSprite != null) ? Vector4.zero : DataUtility.GetOuterUV(activeSprite);
            var c = color;
            vh.Clear();
            vh.AddVert(new Vector3(drawingDimensions.x, drawingDimensions.y), c, new Vector2(sv1.x, sv1.y), new Vector2(1, 0), mDefaultNormal, mDefaultTangent);
            vh.AddVert(new Vector3(drawingDimensions.x, drawingDimensions.w), c, new Vector2(sv1.x, sv1.w), new Vector2(1, 1), mDefaultNormal, mDefaultTangent);
            vh.AddVert(new Vector3(drawingDimensions.z, drawingDimensions.w), c, new Vector2(sv1.z, sv1.w), new Vector2(0, 1), mDefaultNormal, mDefaultTangent);
            vh.AddVert(new Vector3(drawingDimensions.z, drawingDimensions.y), c, new Vector2(sv1.z, sv1.y), new Vector2(0, 0), mDefaultNormal, mDefaultTangent);
            vh.AddTriangle(0, 1, 2);
            vh.AddTriangle(2, 3, 0);
        }

        private Vector4 GetDrawingDimensions(bool shouldPreserveAspect)
        {
            var activeSprite = ActiveSprite;
            Vector4 vector4_1 = !(activeSprite == null) ? DataUtility.GetPadding(activeSprite) : Vector4.zero;
            Vector2 vector2 = !(activeSprite == null) ? new Vector2(activeSprite.rect.width, activeSprite.rect.height) : Vector2.zero;
            Rect pixelAdjustedRect = this.GetPixelAdjustedRect();
            int num1 = Mathf.RoundToInt(vector2.x);
            int num2 = Mathf.RoundToInt(vector2.y);
            Vector4 vector4_2 = new Vector4(vector4_1.x / (float) num1, vector4_1.y / (float) num2, ((float) num1 - vector4_1.z) / (float) num1, ((float) num2 - vector4_1.w) / (float) num2);
            if (shouldPreserveAspect && (double) vector2.sqrMagnitude > 0.0)
            {
                float num3 = vector2.x / vector2.y;
                float num4 = pixelAdjustedRect.width / pixelAdjustedRect.height;
                if ((double) num3 > (double) num4)
                {
                    float height = pixelAdjustedRect.height;
                    pixelAdjustedRect.height = pixelAdjustedRect.width * (1f / num3);
                    pixelAdjustedRect.y += (height - pixelAdjustedRect.height) * this.rectTransform.pivot.y;
                }
                else
                {
                    float width = pixelAdjustedRect.width;
                    pixelAdjustedRect.width = pixelAdjustedRect.height * num3;
                    pixelAdjustedRect.x += (width - pixelAdjustedRect.width) * this.rectTransform.pivot.x;
                }
            }
            vector4_2 = new Vector4(pixelAdjustedRect.x + pixelAdjustedRect.width * vector4_2.x, pixelAdjustedRect.y + pixelAdjustedRect.height * vector4_2.y, pixelAdjustedRect.x + pixelAdjustedRect.width * vector4_2.z, pixelAdjustedRect.y + pixelAdjustedRect.height * vector4_2.w);
            return vector4_2;
        }

        private static readonly Vector3 mDefaultNormal = Vector3.back;
        private static readonly Vector4 mDefaultTangent = new Vector4(1f, 0.0f, 0.0f, -1f);
    }
}